package com.zadtek.jpsqishi.activity;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.Resources;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TableRow;
import android.widget.TextView;

import com.zadtek.jpsqishi.R;
import com.zadtek.jpsqishi.activity.common.Common;
import com.zadtek.jpsqishi.activity.data.PaihData;
import com.zadtek.jpsqishi.frame.JsonProcessHelper;
import com.zadtek.jpsqishi.views.PullDownView;
import com.zadtek.jpsqishi.views.PullDownView.OnPullDownListener;
import com.zadtek.jpsqishi.views.ScrollOverListView;

/**
 * 排行榜child View fragment
 * @author kuyukeji
 *
 */
public class PaihChildFragment extends Fragment{

	private Context context;
	private LayoutInflater inflater;
	private Activity myActivity;
	private Resources res;

	private PullDownView pullDownView;
	private ScrollOverListView listView;
	
	private LinearLayout nolay;		//空数据

	public ArrayList<PaihData> orderLists; // 订单的列表
	private ArrayList<PaihData> initList = null; // 点击和刷新的列表
	public ThreadAdapter threadAdapter;

	private SharedPreferences sp;
	private String state = "3"; // 订单状态(1.配送评分;2.总单量;3.配送准时率)
	
	private TextView psthreeview;	//第3个表格的标题
	
	private String time = Common.getToday();	//获取今天的日期

	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
	}

	// 设置订单的状态
	public void setState(String state) {
		this.state = state;
	}
	
	//设置选择的月份
	public void setTime(String time) {
		this.time = time;
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.fragment_paih_child, null);
		context = view.getContext();
		this.inflater = inflater;
		myActivity = getActivity();
		res = getResources();
		
		pullDownView = (PullDownView) view.findViewById(R.id.pulldownview);
		pullDownView.enableAutoFetchMore(true, 0);
		listView = pullDownView.getListView();
		listView.setSelector(new ColorDrawable(Color.TRANSPARENT));
		
		psthreeview = (TextView) view.findViewById(R.id.psthreeview);
		if (state != null) {
			if (state.equals("2")) { // 总单量
				psthreeview.setText("配送单数");
			} else if(state.equals("3")) {	//准时率
				psthreeview.setText("配送准时率");
			}
		}
		
		//没有订单
		nolay = (LinearLayout) view.findViewById(R.id.nolay);

		threadAdapter = new ThreadAdapter();
		listView.setAdapter(threadAdapter);

		getinitArray();

		pullDownView.setOnPullDownListener(new OnPullDownListener() {
			@Override
			public void onRefresh() {
				// TODO Auto-generated method stub
				initArrays(new Handler() {
					@Override
					public void handleMessage(Message msg) {
						if (!"emp".equals(msg.obj.toString())) {
							orderLists = (ArrayList<PaihData>) msg.obj;
							threadAdapter.notifyDataSetChanged();
							if (orderLists.size() > 0) {
								if (orderLists.get(0).getStatus().equals("1")) {
									nolay.setVisibility(View.GONE);
									pullDownView.setVisibility(View.VISIBLE);
								} else {
									Common.DisplayToast(context, orderLists.get(0)
											.getFail(), 1);
									// 没有订单
									nolay.setVisibility(View.VISIBLE);
									pullDownView.setVisibility(View.GONE);
								}
							}
						} else {
							Common.DisplayToast(context, "通信失败,请检查网络!", 1);
						}
						pullDownView.notifyDidRefresh(true);
						pullDownView.notifyDidLoadMore(true);
					}
				});
			}

			@Override
			public void onLoadMore() {
				// TODO Auto-generated method stub
			}
		});

		return view;
	}

	// 获取初始化数据
	public void getinitArray() {
		initArrays(new Handler() {
			@Override
			public void handleMessage(Message msg) {
				if (!"emp".equals(msg.obj.toString())) {
					orderLists = (ArrayList<PaihData>) msg.obj;
					threadAdapter.notifyDataSetChanged();

					pullDownView.notifyDidDataLoad(false);

					if (orderLists.size() > 0) {
						if (orderLists.get(0).getStatus().equals("1")) {
							nolay.setVisibility(View.GONE);
							pullDownView.setVisibility(View.VISIBLE);
						} else {
							Common.DisplayToast(context, orderLists.get(0)
									.getFail(), 1);
							// 没有订单
							nolay.setVisibility(View.VISIBLE);
							pullDownView.setVisibility(View.GONE);
						}
					}
					pullDownView.notifyDidLoadMore(true);
					
				} else {
					nolay.setVisibility(View.GONE);
					pullDownView.setVisibility(View.GONE);

					pullDownView.notifyDidDataLoad(false);
					if (initList == null) {
						Common.DisplayToast(context, "通信失败,请检查网络!", 1);
					} else {
						pullDownView.notifyDidLoadMore(true);
					}
				}
			}
		});
	}
	
	// 线程加载数据
	private void initArrays(final Handler handler) {
		new Thread(new Runnable() {

			@Override
			public void run() {
				try {
					initList = JsonProcessHelper.jsonProcess_getqstotalph(
							"get_qishi_total", time, state);
					if (initList != null) {
						handler.obtainMessage(0, initList).sendToTarget();
					} else {
						handler.obtainMessage(0, "emp").sendToTarget();
					}

				} catch (Exception e) {
					Thread.interrupted();
					e.printStackTrace();
				}
			}
		}).start();
	}


	public class ThreadAdapter extends BaseAdapter {

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return orderLists == null ? 0 : orderLists.size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public View getView(final int position, View convertView,
				ViewGroup parent) {
			// TODO Auto-generated method stub
			ViewHolder holder = null;
			if (convertView == null) {
				holder = new ViewHolder();
				convertView = inflater.inflate(
						R.layout.fragment_paih_childitem, null);
				
				holder.tablerowid = (TableRow) convertView
						.findViewById(R.id.tablerowid);
				holder.paimlay = (LinearLayout) convertView
						.findViewById(R.id.paimlay);
				holder.paimimgid = (ImageView) convertView
						.findViewById(R.id.paimimgid);
				holder.paimid = (TextView) convertView
						.findViewById(R.id.paimid);
				holder.psname = (TextView) convertView
						.findViewById(R.id.psname);
				holder.psrarea = (TextView) convertView
						.findViewById(R.id.psrarea);
				holder.psscore = (TextView) convertView
						.findViewById(R.id.psscore);

				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}

			if (orderLists.size() > position) {
				if(position %2 == 0) {
					holder.tablerowid.setBackgroundColor(res.getColor(R.color.paih_bg));
				} else {
					holder.tablerowid.setBackgroundColor(res.getColor(R.color.white));
				}
					
				PaihData data = orderLists.get(position);
				String sort_order = data.getSort_order();
				if (sort_order != null && !sort_order.equals("")
						&& Integer.parseInt(sort_order) > 3) {
					holder.paimlay.setVisibility(View.GONE);
					holder.paimid.setVisibility(View.VISIBLE);
					holder.paimid.setText("No."+sort_order);
				} else if(sort_order!=null){
					holder.paimid.setVisibility(View.GONE);
					holder.paimlay.setVisibility(View.VISIBLE);
					if(sort_order.equals("1")) {
						holder.paimimgid.setImageResource(R.drawable.jin);
					} else if(sort_order.equals("2")) {
						holder.paimimgid.setImageResource(R.drawable.yin);
					} else if(sort_order.equals("3")) {
						holder.paimimgid.setImageResource(R.drawable.tong);
					}
				}
				holder.psname.setText(data.getRealname());
				holder.psrarea.setText(data.getArea_name());
				holder.psscore.setText(data.getTotal());
			}
			
			return convertView;
		}

	}
	
	static class ViewHolder {
		private TableRow tablerowid; // row 
		private LinearLayout paimlay;	//排名奖杯
		private ImageView paimimgid;	//奖杯图标
		private TextView paimid; // 排名
		private TextView psname; // 配送人
		private TextView psrarea; // 配送区域
		private TextView psscore; // 配送评分
	}

}
